import tkinter as tk
from tkinter.ttk import *
from tkinter import *
from PIL import Image,ImageTk
import os,time,random

image_rate = 0.6
tScroll = None
tList = None

class PSCObject:
    def __init__(self,no:str='xxxx', sex:str='男', age:int = 22, residence_time:float =0 ):
        self.no = no
        self.sex =sex
        self.age = age
        self.residence_time = residence_time

def resize(w, h, w_box, h_box, pil_image):  
  f1 = image_rate * w_box/w 
  f2 = image_rate * h_box/h  
  factor = min([f1, f2])  
  width = int(w*factor)
  height = int(h*factor)
  return pil_image.resize((width, height),  Image.LANCZOS)  

w_box = 800  
h_box = 600

window = tk.Tk()
window.title('人群分析')

# window.resizable(width=False,height=False) #禁止拖拽窗口 

window.geometry(f'{w_box}x{h_box}')

top_frame = Frame(window,width=w_box,height=100)
top_frame.pack(fill='x',padx=w_box/5,pady=h_box/12)
Label(top_frame,text='照片',font=24).pack(side='left')
Label(top_frame,text='分析信息',font=24).pack(side='right')

content_frame = Frame(window,width=w_box,height=100)
content_frame.pack(fill='x')

left_frame = Frame(content_frame,width=w_box/2.8,height=100)
right_frame = Frame(content_frame,width=w_box/1.2,height=100)

left_frame.pack(side='left',fill='x')
right_frame.pack(side='right',fill='x')

image_label = Label(left_frame)
image_label.pack()

def update_image():
    global tk_image
    pil_image = Image.open('new.png')
    w, h = pil_image.size 
    pil_image_resized = resize(w, h, w_box, h_box, pil_image)
    tk_image = ImageTk.PhotoImage(pil_image_resized)
    image_label.config(image=tk_image)

update_image()

data = [PSCObject() for _ in range(20)]
for i in range(20):
    if i%2 == 0:
        data[i].sex = '男'
    else:
        data[i]
    data[i].age = random.randint(20,50)
    data[i].residence_time = random.randint(1,100)

tScroll=tk.Scrollbar(window, orient=tk.VERTICAL)
tList=tk.Listbox(window,selectmode=tk.BROWSE,font=24,yscrollcommand=tScroll.set)

for item in data:
    no_str = f'编号: {item.no}'
    tList.insert(tk.END,no_str+' ')
    tList.insert(tk.END,' ')
    info_str = f'性别: {item.sex} 年龄: {item.age} 停留时间: {item.residence_time}'
    tList.insert(tk.END,info_str+' ')
    tList.insert(tk.END,' ')
    tList.insert(tk.END,' ')
    tList.insert(tk.END,' ')
    
tScroll.config(command=tList.yview)
footer_frame = Frame(window,width=w_box,height=100)
footer_frame.pack(side='bottom',fill='x')

Button(footer_frame,text='结束').pack(side='right')
Button(footer_frame,text='开始').pack(side='right')

def on_window_resize(event):
    global image_rate,w_box,h_box
    width,height = event.width,event.height
    print(width,height)
    if width >= 1920 and height >= 1000:
        w_box = 1920
        h_box = 1080
        image_rate = 0.8
        update_image()
        tScroll.place(x=1900,y=120,height=760)
        tList.place(x=1500,y=120,width=400,height=760)
        
    elif width == 800 and height == 600:
        image_rate = 0.6
        w_box = 800
        h_box = 600
        update_image()
        tScroll.place(x=780,y=125,height=360)
        tList.place(x=510,y=125,width=270,height=360)
        
window.bind('<Configure>',on_window_resize)
window.mainloop()